package com.hekang.buttontest;

import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity{
    private EditText name;
    private RadioGroup rg;
    private RadioButton rb_Male,rb_FeMale;
    private CheckBox cb_hot,cb_seafood,cb_acid;
    private SeekBar sb_Budget;
    private Button search,details;
    private ImageView searchImg;
    private List<Food> list_food;
    private List<Food> list_get;
    private Person person;
    private MyRadioButtonListnenr myRadioButtonListnenr;
    private MyCheckBox myCheckBox;
    private MySeekBar mySeekBar;

    //保存用户选择
    private boolean hot;
    private boolean seafood;
    private boolean acid;
    private int price;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        initData();
        setListener();
    }

    /**
     * 初始化数据
     */
    private void initData(){
        person = new Person();
        list_food = new ArrayList<Food>();
        list_food.add(new Food("麻辣香锅", 55, R.mipmap.malaxiangguo, true,false, false));
        list_food.add(new Food("水煮鱼", 48, R.mipmap.shuizhuyu, true, true,false));
        list_food.add(new Food("麻辣火锅", 80, R.mipmap.malahuoguo, true, true,false));
        list_food.add(new Food("清蒸鲈鱼", 68, R.mipmap.qingzhengluyu, false,true, false));
        list_food.add(new Food("桂林米粉", 15, R.mipmap.guilin, false, false,false));
        list_food.add(new Food("上汤娃娃菜", 28, R.mipmap.wawacai, false, false,false));
        list_food.add(new Food("红烧肉", 60, R.mipmap.hongshaorou, false,false, false));
        list_food.add(new Food("木须肉", 40, R.mipmap.muxurou, false, false,false));
        list_food.add(new Food("酸菜牛肉面", 35, R.mipmap.suncainiuroumian,false, false, true));
        list_food.add(new Food("西芹炒百合", 38, R.mipmap.xiqin, false, false,false));
        list_food.add(new Food("酸辣汤", 40, R.mipmap.suanlatang, true, false,true));
    }

    /**
     * 初始化控件
     */
    private void init(){
        //输入姓名
        name = (EditText)findViewById(R.id.name);

        //单选按钮
        rg = (RadioGroup)findViewById(R.id.rg_sex);
        rb_Male = (RadioButton)findViewById(R.id.rb_Male);
        rb_FeMale = (RadioButton)findViewById(R.id.rb_FeMale);

        //复选框
        cb_hot = (CheckBox)findViewById(R.id.cb_hot);
        cb_seafood = (CheckBox)findViewById(R.id.cb_seafood);
        cb_acid = (CheckBox)findViewById(R.id.cb_acid);

        //预算条
        sb_Budget = (SeekBar)findViewById(R.id.sb_Budget);

        //按钮
        search = (Button)findViewById(R.id.search);
        details = (Button)findViewById(R.id.details);

        //图片显示
        searchImg = (ImageView)findViewById(R.id.searchImg);
    }

    //绑定事件
    public void setListener(){
        MyButtonListener myButtonListener = new MyButtonListener();
        search.setOnClickListener(myButtonListener);
        details.setOnClickListener(myButtonListener);
        //单选按钮
        myRadioButtonListnenr = new MyRadioButtonListnenr();
        rg.setOnCheckedChangeListener(myRadioButtonListnenr);
        //多选
        myCheckBox = new MyCheckBox();
        cb_hot.setOnCheckedChangeListener(myCheckBox);
        cb_seafood.setOnCheckedChangeListener(myCheckBox);
        cb_acid.setOnCheckedChangeListener(myCheckBox);
        //预算拖拉进度条
        mySeekBar = new MySeekBar();
        sb_Budget.setOnSeekBarChangeListener(mySeekBar);

    }

    //预算拖拉进度条
    class MySeekBar implements SeekBar.OnSeekBarChangeListener{

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            price = seekBar.getProgress();
            Toast.makeText(MainActivity.this,price+"",Toast.LENGTH_LONG).show();
        }
    }




    //多选按钮
    class MyCheckBox implements CompoundButton.OnCheckedChangeListener{

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            CheckBox checkBox = (CheckBox)buttonView;
            switch (checkBox.getId()){
                case R.id.cb_hot:
                    if(isChecked){
                        hot = true;
                    }else{
                        hot = false;
                    }
                    break;
                case R.id.cb_seafood:
                    if(isChecked){
                        seafood = true;
                    }else{
                        seafood = false;
                    }
                    break;
                case R.id.cb_acid:
                    if(isChecked){
                        acid = true;
                    }else{
                        acid = false;
                    }
                    break;
            }
            Toast.makeText(MainActivity.this,hot+"--"+seafood+"--"+acid,Toast.LENGTH_LONG).show();
        }
    }


    //单选按钮
    class MyRadioButtonListnenr implements RadioGroup.OnCheckedChangeListener{

        @Override
        public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
            switch (checkedId){
                case R.id.rb_Male:
                    person.setSex("男");
                    Toast.makeText(MainActivity.this,"男",Toast.LENGTH_LONG).show();
                    break;
                case R.id.rb_FeMale:
                    person.setSex("女");
                    Toast.makeText(MainActivity.this,"女",Toast.LENGTH_LONG).show();
                    break;
            }
        }
    }



    //按钮点击
    class MyButtonListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.search:
                    //搜索
                    clickData();
                    break;
                case R.id.details:
                    //详情
                    Toast.makeText(MainActivity.this,"详情",Toast.LENGTH_LONG).show();
                    break;
            }
        }
    }

    //搜索
    private void clickData(){
        list_get =  new ArrayList<Food>();
        for (int i = 0;i < list_food.size();i++){
            Food food = list_food.get(i);
            if((food.getPrice()<=price)&&(food.isCb_hot()==hot)&&(food.isCb_acid()==acid)&&(food.isCb_seafood()==seafood)){
                //找到数据
                list_get.add(food);
            }
        }
        Toast.makeText(MainActivity.this,"找到数据："+list_get.size(),Toast.LENGTH_LONG).show();
    }
}
